<script setup lang="ts">
import { onHide, onLaunch, onShow } from '@dcloudio/uni-app'
import { navigateToInterceptor } from '@/router/interceptor'
import { useConsoleStore } from '@/store/console'
import { useUserStore } from '@/store/user'

const consoleStore = useConsoleStore()
const userStore = useUserStore()

onLaunch((options) => {
  console.log('App Launch', options)
  // 如果存在sassToken就进行请求用户信息
  if (options?.referrerInfo?.extraData?.sassToken) {
    // 存储token到本地，便于http请求使用
    uni.setStorageSync('token', options?.referrerInfo?.extraData?.sassToken)
    // 计算并存储过期时间
    const now = Date.now()
    const expireTime = now + options?.referrerInfo?.extraData?.tokenTimeout * 1000
    uni.setStorageSync('tokenExpireTime', expireTime)
    userStore.fetchUserInfo()
  }
  consoleStore.addLog('launch', JSON.stringify(options))
})
onShow((options) => {
  console.log('App Show', options)
  consoleStore.addLog('show', JSON.stringify(options))
  // 处理直接进入页面路由的情况：如h5直接输入路由、微信小程序分享后进入等
  // https://github.com/unibest-tech/unibest/issues/192
  if (options?.path) {
    navigateToInterceptor.invoke({ url: `/${options.path}`, query: options.query })
  }
  else {
    navigateToInterceptor.invoke({ url: '/' })
  }
})
onHide(() => {
  console.log('App Hide')
})
</script>

<style lang="scss">
swiper,
scroll-view {
  flex: 1;
  height: 100%;
  overflow: hidden;
}

image {
  width: 100%;
  height: 100%;
  vertical-align: middle;
}
view {
  box-sizing: border-box !important;
}
</style>
